package top.itaso.app;

import android.content.Context;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import top.itaso.lib.common.ToastUtils;

/**
 * @Description:
 * @Author: dengfu.su
 * @CreateDate: 2022/8/4
 */
public abstract class BaseFragment extends Fragment {
    public final String TAG = this.getClass().getSimpleName();
    public Context mContext;

    /**
     * 保证同一按钮在 MIN_CLICK_DELAY_TIME 时间内只响应一次点击事件
     */
    public abstract class OnSingleClickListener implements View.OnClickListener {
        //两次点击按钮的最小间隔，目前为1000
        private static final int MIN_CLICK_DELAY_TIME = 500;
        private long lastClickTime;

        public abstract void onSingleClick(View view);

        @Override
        public void onClick(View v) {
            long curClickTime = System.currentTimeMillis();
            if ((curClickTime - lastClickTime) >= MIN_CLICK_DELAY_TIME) {
                lastClickTime = curClickTime;
                onSingleClick(v);
            }
        }
    }

    /**
     * 同一按钮在短时间内可重复响应点击事件
     */
    public abstract class OnMultiClickListener implements View.OnClickListener {
        public abstract void onMultiClick(View view);

        @Override
        public void onClick(View v) {
            onMultiClick(v);
        }
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        mContext = context;
        Log.v(TAG, "onAttach: >>>>>>context=" + mContext);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.v(TAG, "onCreate: >>>>>>" + TAG);
    }


    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        int layoutId = layoutId();
        if (layoutId == 0) {
            try {
                throw new Exception("请实现 xml 布局文件");
            } catch (Exception e) {
            }
            return null;
        }
        View view = inflater.inflate(layoutId(), container, false);
        Log.v(TAG, "onCreateView: >>>>>>view=" + view);
        initView(view);
        initData(mContext);
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        Log.v(TAG, "onStart: >>>>>>");
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.v(TAG, "onResume: >>>>>>");
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.v(TAG, "onPause: >>>>>>");
    }

    @Override
    public void onStop() {
        super.onStop();
        Log.v(TAG, "onStop: >>>>>>");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Log.v(TAG, "onDestroyView: >>>>>>");
        mContext = null;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.v(TAG, "onDestroy: >>>>>>");
    }

    @Override
    public void onDetach() {
        super.onDetach();
        Log.v(TAG, "onDetach: >>>>>>");
    }

    /**
     * 初始化布局:R.layout.xxx
     * 优先级低于 layoutView()
     *
     * @return 布局 id
     */
    protected abstract int layoutId();

    /**
     * 初始化控件
     *
     * @param view 布局view
     */
    protected abstract void initView(View view);

    /**
     * 初始化，绑定数据
     *
     * @param context 上下文
     */
    protected abstract void initData(Context context);


    /**
     * 显示提示  toast
     *
     * @param msg 提示信息
     */
    public void showToast(String msg) {
        try {
            ToastUtils.getInstance().showShortToast(mContext, msg);
        } catch (Exception e) {
            e.printStackTrace();
            //解决在子线程中调用Toast的异常情况处理
            Looper.prepare();
            ToastUtils.getInstance().showShortToast(mContext, msg);
            Looper.loop();
        }
    }
}
